var settingshow = false;
var islog = false;
var ws;

$(document).ready(function() {
     
    $("#search").click(function() {
		if ($("#url").val() == "") {
	        var node = $("#message").css("display", "block");
	        node.append('<span style="line-height: 50px; font-size: 14px; color: #000;">请输入要抓取的页面URL地址</span>');
	        return;
	    }
		
		$("#ws_result").slideUp();
		$("#aj_pre").attr("href", "");
		$("#aj_de").html("");
		$("#aj_se").html("");
		$(".ui form").addClass("loading");
		$("#message").html("");
		
		if (islog) {
			socket($("#url").val());
		} else {
			ajax($("#url").val());
		}
    });

    $("#url").click(function() {
        $("#message").html("").css("display", "none");
    });
	
	$("#aj_close").click(function() {
		$("#aj_result").slideUp();
	});
	
	$("#setting").click(function() {
		var s = $('#settingpan');
		if (!settingshow) {
			$(s).slideDown();
		} else {
			$(s).slideUp();
		}
		settingshow = !settingshow;
	});
	
	$("#iscookie").checkbox({
		onChecked: function() {
			
		}
	});
	
    $('#control-panel').click(function () {
        $(this).blur();
        $('#setting_modals').modal('show');
        event.preventDefault();
    });
	
	$("#islog").checkbox({
		onChecked: function() {
			islog = true;
			if (ws == null) {
				ws = ws = new WebSocket("ws://localhost/searchsocket");
			}
			ws.onopen = function(e) {
				$("#log").html("").append("抓取详细信息记录已开启。。。&#13;&#10;");
			}
			ws.onmessage = function(e) {
				var msg = e.data;
				message(msg);
			}
			ws.onerror = function(e) {
				$("#log").append("服务器链接发生异常，请F5刷新页面后再试!");
			}
			ws.close = function(e) {
				$("#log").append("您已经与服务器断开链接，请F5刷新页面后再试!");
			}
		},
		onUnchecked: function() {
			islog = false;
			ws.close();
			ws = null;
		}
	});
	
	$("#aj_down").click(function(){
		zipdown($(this).attr("pid"), $(this).attr("zipurl"), this);
	});
	
	$("#ws_down").click(function(){
		zipdown($(this).attr("pid"), $(this).attr("zipurl"), this);
	});
	
	$("#like").click(function() {
		$.ajax({
			url: 'like',
			type: 'post',
			dataType: 'json',
			success: function(data) {
				if (data.code == 0) {
					$("#likenumber").html(data.num);
				}
			}
		})
	});
	
    $('.ui.accordion').accordion();
	$('#l').popup();
	
});

function ajax(url) {
    $.ajax({
        url: 'searchajax',
        type: 'post',
        data: {'url': url},
        dataType: 'json',
        success: function(data) {
            if (data.code != 0) {
                $("#message").css("display", "block").html('<span style="line-height: 50px; font-size: 14px; color: #000;">' + data.message + '</span>');
                return;
            }
			$("#aj_result").slideDown();
			$("#aj_pre").attr("href", data.data.preurl);
			$("#aj_down").attr("zipurl", data.data.zipurl).attr("pid", data.data.pid);
			if ( data.data.de != undefined ){
				var aj_de = $("#aj_de");
				$.each(data.data.de, function(i, v) {
					aj_de.append(v + "&#13;&#10;");
				})
			}
			if ( data.data.se != undefined ){
			var aj_se = $("#aj_se");
				$.each( data.data.se, function(i, v) {
					aj_se.append(v + "&#13;&#10;");
				})
			}
        },
        error: function(e) {
            var node = $("#message").css("display", "block");
            node.append('<span style="line-height: 50px; font-size: 14px; color: #000;">服务器发生异常，请稍后再试！</span>');
        },
        complete: function(r, s) {
            $(".ui form").removeClass("loading");
        }
    });
}

function socket(url) {
	
    ws.send(url);
}

function message(msg) {
	var log = $("#log");
	log.append(msg + "&#13;&#10;");

	$("#log").scrollTop(100000);
	if (msg.indexOf("end") != -1) {
		$(".ui form").removeClass("loading");
	} else if (msg.indexOf("{") == 0) {
		$("#ws_result").slideDown();
		var obj = eval('(' + msg + ')');
		$("#ws_pre").attr("href", obj.preurl);
		$("#ws_down").attr("zipurl", obj.zipurl).attr("pid", obj.pid);
	}
}

function zipdown(pid, url, c) {
	$.ajax({
		url: "/persistentid?pid=" + pid,
		type: 'get',
		dataType: 'json',
		beforeSend: function(e) {
			$(c).addClass("loading");
			$('#querydown').html("");
		},
		success: function(data) {
			if (data.code != 0) {
				$('#querydown').html('<div class="content" style="text-align: center;">' +
					'<i class="hourglass half icon" style="font-size: 2em; margin-bottom: 20px;"></i>' +
					'<p>' + data.message +　'</p>' +
					'</div>');
			} else {
				$('#querydown').html('<div class="content" style="text-align: center;">' +
					'<i class="file icon" style="font-size: 2em; margin-bottom: 20px;"></i>' +
					'<p>资源压缩完成, 请点击&nbsp;<a href="' + url + '">下载资源</a>&nbsp;将资源下载到您的电脑</p>' +
					'</div>');
			}
			$('#querydown').modal('show');
		},
		error: function(e) {
			alert("服务压缩异常");
		},
		complete: function(e) {
			$(c).removeClass("loading");
		}
	});
}